package com.ruoyi.products.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.products.domain.Img;
import com.ruoyi.products.service.IImgService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.entity.SysUser;

/**
 * 选品图片Controller
 *
 * @author star
 * @date 2025-06-04
 */
@RestController
@RequestMapping("/products/img")
public class ImgController extends BaseController
{
    @Autowired
    private IImgService imgService;

    /**
     * 导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Img> util = new ExcelUtil<Img>(Img.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @Log(title = "商品导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Img> util = new ExcelUtil<Img>(Img.class);
        List<Img> userList = util.importExcel(file.getInputStream());

        for (Img img : userList) {
            imgService.insertImg(img);
        }

        //todo 把解析的数据 导入数据库
        return success("成功");
    }

    /**
     * 查询选品图片列表
     */
    @PreAuthorize("@ss.hasPermi('products:img:list')")
    @GetMapping("/list")
    public TableDataInfo list(Img img)
    {
        startPage();
        List<Img> list = imgService.selectImgList(img);
        return getDataTable(list);
    }

    /**
     * 导出选品图片列表
     */
    @PreAuthorize("@ss.hasPermi('products:img:export')")
    @Log(title = "选品图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Img img)
    {
        List<Img> list = imgService.selectImgList(img);
        ExcelUtil<Img> util = new ExcelUtil<Img>(Img.class);
        util.exportExcel(response, list, "选品图片数据");
    }

    /**
     * 获取选品图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('products:img:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imgService.selectImgById(id));
    }

    /**
     * 新增选品图片
     */
    @PreAuthorize("@ss.hasPermi('products:img:add')")
    @Log(title = "选品图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Img img)
    {
        return toAjax(imgService.insertImg(img));
    }

    /**
     * 修改选品图片
     */
    @PreAuthorize("@ss.hasPermi('products:img:edit')")
    @Log(title = "选品图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Img img)
    {
        return toAjax(imgService.updateImg(img));
    }

    /**
     * 删除选品图片
     */
    @PreAuthorize("@ss.hasPermi('products:img:remove')")
    @Log(title = "选品图片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imgService.deleteImgByIds(ids));
    }
}
